    define(['zepto', 'butterfly/view','../../bsl',
        'text!./camera.html'
    ], function($, View, bsl, camera) {
        var me = null;

        var view = View.extend({
            events: {

                // ！！！！ 由于 引入 touch.js 后，tap事件会变成双击，所以 本页面内不能使用tap，用click代替。
                "tap #getImage":"getImage",
                "tap #getVideo":"getVideo",
                "tap #cleanHistory":"cleanHistory",
                "tap #openDoc" : "openDoc",
                "tap #back" : "back",
                
          
            },
            render: function() {
                me = this;
            },

            onShow: function() {
           

                // 绑定自定义事件 滑动切换日历等
        
        this.domcontentload();
             this.plusReady(); 


            },
            i:1,
            gentry:null,
            w:null,
            hl:null,
            le:null,
            de:null,
            ie:null,
            unv:true,
    // H5 plus事件处理
    plusReady:function (){
        var me=this;
        // 获取摄像头目录对象
//        bsl.io.resolveLocalFileSystemURL( "_doc/", function ( entry ) {
//            entry.getDirectory( "camera", {create:true}, function ( dir ) {
//                gentry = dir;
//                me.updateHistory();
//            }, function ( e ) {
//                 console.info( "Get directory \"camera\" failed: "+e.message );
//            } );
//        }, function ( e ) {
//             console.info( "Resolve \"_doc/\" failed: "+e.message );
//        } );
    },
            // 监听DOMContentLoaded事件
            domcontentload:function(){
        document.addEventListener("DOMContentLoaded",function(){
            // 获取DOM元素对象
            hl=document.getElementById("history");
            le=document.getElementById("empty");
            de=document.getElementById("display");
            if(ie=document.getElementById("index")){
                ie.onchange=indexChanged;
            }
            // 判断是否支持video标签
            unv=!document.createElement('video').canPlayType;
        },false );
        },
    
    changeIndex:function () {
         console.info( "选择摄像头：" );
        this.ie.focus();
    },
    indexChanged:function () {
        this.de.innerText = ie.options[ie.selectedIndex].innerText;
        this.i = parseInt( ie.value );
         console.info( de.innerText );
    },
    // 拍照
    getImage: function () {
        console.info( "开始拍照：" );
        var cmr = bsl.camera.getCamera();
        cmr.captureImage( function ( p ) {
           
             console.info( "成功："+p );
//            bsl.io.resolveLocalFileSystemURL( p, function ( entry ) {
//                createItem( entry );
//            }, function ( e ) {
//                console.info( "读取拍照文件错误："+e.message );
//            } );
        }, function ( e ) {
             console.info( "失败："+e.message );
        }, {filename:"_doc/camera/",index:1} );
    },
    // 录像
    getVideo:function () {
        var me=this;
        console.info( "开始录像：" );
        var cmr = bsl.camera.getCamera();
        cmr.startVideoCapture( function ( p ) {
             console.info( "成功："+p );
//            bsl.io.resolveLocalFileSystemURL( p, function( entry) {
//                me.createItem( entry );
//            }, function( e ) {
//                 console.info( "读取录像文件错误："+e.message );
//            } );
        }, function( e ){
             console.info( "失败："+e.message );
        }, {filename:"_doc/camera/",index:1} );
                               
                       // 拍摄10s后自动完成
                       setTimeout( function () {
                                  cmr.stopVideoCapture();
                                  }, 10000 );
    },
    // 显示文件
    displayFile:function ( li ) {
        if ( w ) {
            console.info( "重复点击！" );
            return;
        }
        if ( !li || !li.entry ) {
             console.info( "无效的媒体文件" );
            return;
        }
        var name = li.entry.name;
        var suffix = name.substr(name.lastIndexOf('.'));
        var url = "";
        if ( suffix==".mov" || suffix==".3gp" || suffix==".mp4" || suffix==".avi" ){
            //if(unv){plus.runtime.openFile("_doc/camera/"+name);return;}
            url = "/plus/camera_video.html";
        } else {
            url = "/plus/camera_image.html";
        }
        w=plus.webview.create(url,url,{scrollIndicator:'none',scalable:true,bounce:"all"});
        w.addEventListener( "loaded", function(){
            w.evalJS( "loadMedia('"+li.entry.toLocalURL()+"')" );
            //w.evalJS( "loadMedia(\""+"http://localhost:13131/_doc/camera/"+name+"\")" );
        }, false );
        w.addEventListener( "close", function() {
            w = null;
        }, false );
        w.show( "pop-in" );
    },

    // 添加播放项
    createItem:function ( entry ) {
        var me=this;
        var li = document.createElement("li");
        li.className = "ditem";
        li.innerHTML = '<span class="iplay"><font class="aname"></font><br/><font class="ainf"></font></span>';
        li.setAttribute( "onclick", "displayFile(this);" );
        hl.insertBefore( li, le.nextSibling );
        li.querySelector(".aname").innerText = entry.name;
        li.querySelector(".ainf").innerText = "...";
        li.entry = entry;
        me.updateInformation( li );
        // 设置空项不可见
        le.style.display = "none";
    },
    // 获取录音文件信息
    updateInformation:function ( li ) {
        if ( !li || !li.entry ) {
            return;
        }
        var entry = li.entry;
        entry.getMetadata( function ( metadata ) {
            li.querySelector( ".ainf" ).innerText = dateToStr( metadata.modificationTime );
        }, function ( e ) {
             console.info( "获取文件\""+entry.name+"\"信息失败："+e.message );
        } );
    },
    // 获取录音历史列表
    updateHistory:function () {
        if ( !gentry ) {
            return;
        }
        var reader = gentry.createReader();
        reader.readEntries( function ( entries ) {
            for ( var i in entries ) {
                if ( entries[i].isFile ) {
                    createItem( entries[i] );
                }
            }
        }, function ( e ) {
             console.info( "读取录音列表失败："+e.message );
        } );
    },
    // 清除历史记录
    cleanHistory:function () {
        hl.innerHTML = '<li id="empty" class="ditem-empty">无历史记录</li>';
        le = document.getElementById( "empty" );
        // 删除音频文件
         console.info( "清空拍照录像历史记录：" );
        gentry.removeRecursively( function () {
            // Success
             console.info( "成功！" );
        }, function ( e ) {
             console.info( "失败："+e.message );
        });
    },
      back : function(e){
            window.history.go(-1);
        },
         openDoc:function(){
             window.butterfly.navigate('com.no5.cordovademo/doc/camera', {
                trigger: true
            });


        },
          

        });
        return view;
    });
